package com.cwt.admin.manage;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckOr;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cwt.admin.common.BaseController;
import com.cwt.common.result.AjaxResult;
import com.cwt.common.utils.SqlUtils;
import com.cwt.common.vo.PageInfo;
import com.cwt.service.domain.SysRole;
import com.cwt.service.service.SysRoleService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author chenwt
 * @version V1.0
 * @describe 【】
 * @date 2023年08月15日 14:11:12
 */
@Api(tags = "权限管理", value = "RoleController")
@RequestMapping("/role")
@RestController
@SaCheckOr(
        login = @SaCheckLogin,
        role = @SaCheckRole("manage"),
        permission = @SaCheckPermission("system")
)
public class RoleController extends BaseController {

    @Autowired
    private SysRoleService roleService;

    /**
     * 权限数据分页
     *
     * @param info 分页数据信息，及条件信息
     * @return 用户分页数据
     */
    @RequestMapping(value = "/page", method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody
    AjaxResult page(@RequestBody PageInfo info) {
        Page<SysRole> page = new Page<>(info.getPageNum(), info.getPageSize());
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        SqlUtils.wrapperSetLikeCondition(queryWrapper, info.getParams());
        return AjaxResult.success(roleService.page(page, queryWrapper));
    }

    /**
     * 角色新增
     * @param role 角色
     * @return 权限新增
     */
    @RequestMapping(value = "/save", method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody AjaxResult roleSave(@RequestBody SysRole role) {
        // 验证角色名称
        if (!ObjectUtils.isEmpty(roleService.querySysRoleByName(role))) {
            return AjaxResult.error("角色名称【" + role.getName() + "】已存在");
        }

        // 初始化角色信息
        role.setCreatetime(new Date());
        role.setUpdatetime(new Date());

        // 保存角色
        roleService.save(role);

        // 保存角色权限菜单
        roleService.insertRoleMenu(role);
        return AjaxResult.success("角色【" + role.getName() + "】新增成功");
    }

    /**
     * 角色修改
     * @param role 角色
     * @return 角色修改
     */
    @RequestMapping(value = "/edit", method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody AjaxResult roleEdit(@RequestBody SysRole role) {
        if (!roleService.checkRoleNameUnique(role)) {
            return AjaxResult.error("修改角色【" + role.getName() + "】失败，角色名称已存在");
        }

        // 初始化配置
        role.setUpdatetime(new Date());
        roleService.updateById(role);
        return AjaxResult.success(roleService.updateRoleMenu(role));
    }

}
